package com.firefightingiot.firefightingiot.config.scheduling;

import cn.hutool.json.JSONObject;
import com.firefightingiot.firefightingiot.config.rocketmq.UserProducer;
import com.firefightingiot.firefightingiot.support.utils.MyLogger;
import com.firefightingiot.firefightingiot.web.mapper.DeviceBusinessMapper;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;

@Component
public class Scheduling {

    @Resource
    public MyLogger logger;

    @Resource
    DeviceBusinessMapper deviceBusinessMapper;
    @Resource
    UserProducer userProducer;

//    @Scheduled(cron="0 */5 * * * ?")
//    public void updateToOff() {
//        JSONObject jsonParam = new JSONObject();
//        jsonParam.put("deviceFlag","updateToOff");
//        String mqFlag = FirefightingiotKit.getStr("mqFlag","mq");
//        if(mqFlag.equals("1")){
//            updateToOffMethod();
//        }else if(mqFlag.equals("2")){
//            this.provider.sendMessage(jsonParam.toString());
//        }else if(mqFlag.equals("3")){
//            userProducer.produder(jsonParam.toString());
//        }
//    }

    public void updateToOffMethod(){
        logger.info("----------------updateToOffMethod------------------");
        deviceBusinessMapper.updateToOffForFire(new JSONObject());
        deviceBusinessMapper.updateToOffForWater(new JSONObject());
        deviceBusinessMapper.updateToOffForGas(new JSONObject());
    }

    //@Scheduled(cron="0 */1 * * * ?")//一分钟一回
//    @Scheduled(cron="*/20 * * * * ?")//30秒一回
//    public void updateForKillLock() throws Exception {
//        JSONObject jsonParam = new JSONObject();
//        List<String> threadIds = deviceBusinessMapper.getLock(jsonParam);
//        if(!threadIds.isEmpty()){
//            for(String threadId : threadIds){
//                jsonParam.put("threadId",threadId);
//                System.out.println("死锁："+threadId);
//                deviceBusinessMapper.updateForKillLock(jsonParam);
//            }
//        }else{
//            System.out.println("没有死锁");
//        }
//        //System.out.println(contextLoads(System.currentTimeMillis())+"没有死锁");
//
//    }

    /**
     * 将毫秒转化为日期(格式:yyyy-MM-dd hh:mm:ss)
     * @param l
     * @return
     * @throws Exception
     */
    public String contextLoads(Long l) throws Exception {

        java.text.SimpleDateFormat sdf1=new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");
        Date date = new Date(l);
        String str1 = sdf1.format(date);
        java.text.SimpleDateFormat sdf=new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date2 = sdf.parse(str1);
        String str2 = sdf.format(date2);
        return str2;
    }
}
